package cn.iocoder.yudao.module.knowledge.controller.admin.elecdatacontact;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.knowledge.controller.admin.elecdatacontact.vo.*;
import cn.iocoder.yudao.module.knowledge.dal.dataobject.elecdatacontact.ElecDataContactDO;
import cn.iocoder.yudao.module.knowledge.service.elecdatacontact.ElecDataContactService;

@Tag(name = "管理后台 - 用电信息")
@RestController
@RequestMapping("/knowledge/elec-data-contact")
@Validated
public class ElecDataContactController {

    @Resource
    private ElecDataContactService elecDataContactService;

    @PostMapping("/create")
    @Operation(summary = "创建用电信息")
    @PreAuthorize("@ss.hasPermission('knowledge:elec-data-contact:create')")
    public CommonResult<Long> createElecDataContact(@Valid @RequestBody ElecDataContactSaveReqVO createReqVO) {
        return success(elecDataContactService.createElecDataContact(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用电信息")
    @PreAuthorize("@ss.hasPermission('knowledge:elec-data-contact:update')")
    public CommonResult<Boolean> updateElecDataContact(@Valid @RequestBody ElecDataContactSaveReqVO updateReqVO) {
        elecDataContactService.updateElecDataContact(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用电信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('knowledge:elec-data-contact:delete')")
    public CommonResult<Boolean> deleteElecDataContact(@RequestParam("id") Long id) {
        elecDataContactService.deleteElecDataContact(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用电信息")
                @PreAuthorize("@ss.hasPermission('knowledge:elec-data-contact:delete')")
    public CommonResult<Boolean> deleteElecDataContactList(@RequestParam("ids") List<Long> ids) {
        elecDataContactService.deleteElecDataContactListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用电信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('knowledge:elec-data-contact:query')")
    public CommonResult<ElecDataContactRespVO> getElecDataContact(@RequestParam("id") Long id) {
        ElecDataContactDO elecDataContact = elecDataContactService.getElecDataContact(id);
        return success(BeanUtils.toBean(elecDataContact, ElecDataContactRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用电信息分页")
    @PreAuthorize("@ss.hasPermission('knowledge:elec-data-contact:query')")
    public CommonResult<PageResult<ElecDataContactRespVO>> getElecDataContactPage(@Valid ElecDataContactPageReqVO pageReqVO) {
        PageResult<ElecDataContactDO> pageResult = elecDataContactService.getElecDataContactPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ElecDataContactRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用电信息 Excel")
    @PreAuthorize("@ss.hasPermission('knowledge:elec-data-contact:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportElecDataContactExcel(@Valid ElecDataContactPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ElecDataContactDO> list = elecDataContactService.getElecDataContactPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用电信息.xls", "数据", ElecDataContactRespVO.class,
                        BeanUtils.toBean(list, ElecDataContactRespVO.class));
    }

}